CSS Transitions and Animations: A Beginner's Guide
CSS Transitions and Animations allow you to add smooth, visually appealing effects to your web pages. With just a few lines of CSS, you can enhance user experience and make your website feel more interactive and modern — without JavaScript!
What Are CSS Transitions?
CSS Transitions let you change property values smoothly (over a duration) instead of instantly.
Example:
<button class="btn">Hover Me</button>
.btn {
background-color: blue;
color: white;
padding: 10px 20px;
border: none;
transition: background-color 0.3s ease;
}
.btn:hover {
background-color: green;
}
Result:
What Are CSS Animations?
CSS Animations are more powerful than transitions. They can run automatically, loop, and follow sequences using keyframes.
Example:
<div class="box"></div>
.box {
width: 100px;
height: 100px;
background-color: red;
animation: moveBox 2s infinite alternate;
}
@keyframes moveBox {
0% {
transform: translateX(0);
}
100% {
transform: translateX(200px);
}
}
Result:
CSS Transitions vs Animations
| Feature | Transitions | Animations |
|---|---|---|
| Triggered by | User action (e.g., hover) | Can run automatically |
| Keyframes | Not supported | Fully supported |
| Looping | No | Yes |
Best Practices
- Use transitions for hover effects and subtle UI enhancements.
- Use animations for attention-grabbing visuals like loaders or banners.
- Keep effects smooth and not too distracting.
- Ensure accessibility for all users — avoid rapid flashing.
Learn More
Bonus Challenge: Try animating a card flip or fade-in text using only CSS!